//
//  ShoppingCartTableViewCell.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/11.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON
import PKHUD
protocol ShoppingCartTableViewCellDelegate: NSObjectProtocol {
    func selectCell(index: Int, flag: Bool)
    func delCell(id:String,cell:ShoppingCartTableViewCell) -> Void
    
}
class ShoppingCartTableViewCell: UITableViewCell {

    
    @IBOutlet weak var headImage: UIImageView!
    @IBOutlet weak var goodsName: UILabel!
    
    @IBOutlet weak var priceLable: UILabel!
    @IBOutlet weak var modelName: UILabel!
    @IBOutlet weak var selectBtn: UIButton!
    weak var delegate :ShoppingCartTableViewCellDelegate?
    weak var delegate2: UIViewController?
    var deleteblock: deleteBlock?
    var cellIndex :Int = 0
    var carModel: cartModel?
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func bindData(model: cartModel, index: Int) -> Void {
        self.carModel = model
        headImage.sd_setImage(with: appendUrl(suffix: model.imageUrl!), completed: nil)
        goodsName.text = model.goodsName
        let number :String = model.number!
        let spec :String = model.specAndCol!
        modelName.text = "\(spec)*\(number)"
        priceLable.text = "￥：" + model.price!
        var flag :Bool = false
        if model.selected == "1" {
            flag = true
        }
        cellIndex = index
        self.setSelected(flag, animated: true)
        
        for ges in (self.contentView.gestureRecognizers)! {
            if ges.isKind(of: UILongPressGestureRecognizer.self){
                self.contentView.removeGestureRecognizer(ges)
            }
        }
        let long = UILongPressGestureRecognizer.init(target: self, action: #selector(handleLong(long:)))
        self.contentView.isUserInteractionEnabled = true
        self.contentView.addGestureRecognizer(long)
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
    }
    
    @IBAction func selectAct(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        self.setSelected(sender.isSelected, animated: true)
        self.delegate?.selectCell(index: cellIndex, flag: sender.isSelected)
    }
    
    @objc func handleLong(long: UILongPressGestureRecognizer){
        
        if long.state == .began {
            self.delegate?.delCell(id: (self.carModel?.id)!, cell: self)
        }
    }
}
